package cn.masterj.springcloud.controller;

import cn.masterj.springcloud.CommonResult;
import cn.masterj.springcloud.entity.Account;
import cn.masterj.springcloud.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author masterJ
 * @create 2020-04-20 17:40
 */
@RestController
public class AccountController {

    @Autowired
    private IAccountService accountService;

    @GetMapping("/account/getById/{id}")
    public CommonResult<Account> getById(@PathVariable("id") Long id){
        Account account = accountService.getById(id);
        return new CommonResult<>(200,"查询成功", account);
    }

    /**
     * 新增一条金额减少100的记录
     * @param id
     * @return
     */
    @GetMapping("/account/payment/{id}")
    public CommonResult<Account> payment(@PathVariable("id") Long id){
        Account account = accountService.getById(id);
        account.setId(null);
        account.setDecrease(100);
        account.setAmount(account.getBalance());
        account.setBalance(account.getBalance() - 100);

        accountService.save(account);

        try {
            // 模拟超时异常，OpenFeign来调，默认是1秒，这里就肯定会超时异常
            TimeUnit.SECONDS.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new CommonResult<>(200,"新增一条金额减少100的记录成功", account);
    }
}
